package com.synergy.stockflow.production.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.production.create.ReportDetailVo;
import com.synergy.stockflow.production.create.ReportOrTaskVo;
import com.synergy.stockflow.production.entity.*;
import com.synergy.stockflow.production.service.ProductionPlanService;
import com.synergy.stockflow.production.service.ProductionReportDetailService;
import com.synergy.stockflow.production.service.ProductionReportService;
import com.synergy.stockflow.production.service.ProductionTaskService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/report")
public class ProductionReportController {

    @Autowired
    private ProductionReportService productionReportService;

    @Autowired
    private ProductionReportDetailService productionReportDetailService;

    @Autowired
    private ProductionPlanService productionPlanService;

    @Autowired
    private ProductionTaskService productionTaskService;

    @GetMapping("/page")
    public Result page(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "2") int size,
            @RequestParam(name="taskId", required = false)String taskId,
            @RequestParam(name="materialName", required = false)String materialName,
            @RequestParam(name="reportPeopleId", required = false)String reportPeopleId,
            @RequestParam(name="checkId", required = false)String checkId
            ) {
        System.out.println(taskId+materialName+reportPeopleId+checkId);
        QueryWrapper<ProductionReport> queryWrapper = new QueryWrapper<>();
        Page<ProductionReport> objectPage = new Page<>(current, size);
        List<ProductionReport> reportOrTaskVos = productionReportService.selectByReport(objectPage,taskId, materialName,reportPeopleId, checkId);
        objectPage.setRecords(reportOrTaskVos);
        return Result.success(objectPage);
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam(name = "reportId") String reportId) {
        List<ReportDetailVo> reportDetailVos = productionReportDetailService.selectByReportId(reportId);
        return Result.success(reportDetailVos);
    }

    /**
     * 更新报工单信息
     */
    @PutMapping("/update/{reportId}")
    public Result<?> updateReport(@PathVariable String reportId,
                                  @RequestBody ReportOrTaskVo updateDTO) {
        try {
            productionReportService.updateReport(reportId, updateDTO);
            return Result.success(updateDTO);
        } catch (Exception e) {
            return Result.error(404, "更新失败");
        }
    }

    @PutMapping("/batch")
    public Result<?> batchUpdateDetails(@RequestBody List<ProductionReportDetail> details) {
        productionReportDetailService.batchUpdateDetails(details);
        return Result.success(details);
    }


    @PutMapping("/deleteStatus")
    public Result updateByStatus(@RequestParam("reportId") String reportId) {
        ProductionReport byId = productionReportService.getById(reportId);
        if (byId != null && 0 == (byId.getStatus())) {
            byId.setStatus(2);
            productionReportService.updateById(byId);
            return Result.success(byId);
        } else {
            return Result.error(404, "该单子无法删除");
        }
    }

    //批量删除
    @PutMapping("/batchDelete")
    public Result delete(@RequestBody List<ProductionReport> reportId) {
        for (ProductionReport q : reportId) {
            productionReportService.updateListStatus(q.getReportId());
        }
        return Result.success(reportId);
    }


    //生成领料编号
    private String generateReport() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String dateStr = sdf.format(new Date());

        // 查询当月的计划数量
        int count = productionReportService.countCurrentMonthPlans();

        return "RG-" + dateStr + String.format("%03d", count + 1);
    }

    @GetMapping("/planAll")
    private Result planAll() {
        return Result.success(productionPlanService.selectByPlan());
    }

    @GetMapping("/taskAll")
    private Result taskAll() {
        return Result.success(productionTaskService.selectTask());
    }

    @PostMapping("/addReport")
    private Result addReport(@RequestBody ProductionReport report) {
        report.setReportCode(generateReport());
        report.setStartDate(new Date());
        report.setEndDate(report.getReportTime());
        report.setCreateTime(new Date());
        return Result.success(productionReportService.save(report));
    }

    @PutMapping("/updateReport")
    private Result updateReport(@RequestBody ProductionReport report) {
        return Result.success(productionReportService.updateById(report));
    }

    @PutMapping("/task/updateComputedQuantity")
    private Result updateComputedQuantity(@RequestBody Report report) {
        return Result.success(productionTaskService.updateComputedQuantity(report.getTaskId(), report.getComputedQuantity()));
    }

    @DeleteMapping("deleteReport/{reportId}")
    public Result deleteReport(@PathVariable("reportId") String reportId) {
        return Result.success(productionReportService.removeById(reportId));
    }
}
    @Data
    class Report{
        private String taskId;
        private BigDecimal computedQuantity;
    }

